﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CCWin;
using BookModel;
using BLL;
namespace WinFromBook
{
    public partial class FrmReading : CCSkinMain
    {
        public FrmReading()
        {
            InitializeComponent();
        }
        #region 封装操作方法
        ReadingInfoBLL bll = new ReadingInfoBLL();
        //加载所有书架信息
        public void selectReading()
        {
            this.listViewReadingInfo.Items.Clear();
            foreach (var item in bll.GetReading())
            {
                ListViewItem lvi = new ListViewItem(item.RID);
                lvi.SubItems.Add(item.RName);
                lvi.SubItems.Add(item.Raddress);
                lvi.SubItems.Add(item.Remark);
                this.listViewReadingInfo.Items.Add(lvi);
            }
        }
        //清空文本
        public void textClear()
        {
            textBoxID.Text = "";
            textBoxName.Text = "";
            textBoxRaddress.Text = "";
            textBRemark.Text = "";
        }
        //检查文本框是否为空
        public bool TextCheck()
        {
            if (String.IsNullOrEmpty(textBoxReadingID.Text.Trim()))
            {
                MessageBox.Show("书架编号不能为空", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(textBoxName.Text.Trim()))
            {
                MessageBox.Show("书架名称不能为空", "系统提示");
                return false;
            }
            if (String.IsNullOrEmpty(textBoxRaddress.Text.Trim()))
            {
                MessageBox.Show("书架位置不能为空", "系统提示");
                return false;
            }
            return true;
        }
        //检查是否已存在书架
        public bool CheckRID()
        {
            foreach (var I in new BLL.ReadingInfoBLL().GetReading())
            {
                if (I.RID == textBoxID.Text.Trim())
                {
                    MessageBox.Show("该书架已存在记录", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    return false;
                }
            }
            return true;

        }
        //根据名称查询书架
        public void GetName()
        {
            this.listViewReadingInfo.Items.Clear();
            if (String.IsNullOrEmpty(textBoxID.Text.Trim()))
                selectReading();
            else
            {
                foreach (var item in new BLL.ReadingInfoBLL().GetReading())
                {
                    if (item.RName.Contains(textBoxID.Text.Trim()))
                    {
                        ListViewItem iv = new ListViewItem(item.RID);
                        iv.SubItems.Add(item.RName);
                        iv.SubItems.Add(item.Raddress);
                        iv.SubItems.Add(item.Remark);
                        this.listViewReadingInfo.Items.Add(iv);
                    }
                }
            }
            if (this.listViewReadingInfo.Items.Count <= 0)
            {
                MessageBox.Show("不存在该书架", "系统提示");
                selectReading();
            }
        }
        //点击ListView事件
        public void LVClick()
        {
            if (listViewReadingInfo.SelectedItems.Count <= 0)
            {
                return;
            }
            textBoxReadingID.Text = listViewReadingInfo.SelectedItems[0].SubItems[0].Text;
            textBoxName.Text = listViewReadingInfo.SelectedItems[0].SubItems[1].Text;
            textBoxRaddress.Text = listViewReadingInfo.SelectedItems[0].SubItems[2].Text;
            textBRemark.Text = listViewReadingInfo.SelectedItems[0].SubItems[3].Text;
        }
        //新增书架
        public void InsertReading()
        {
            if (!CheckRID())
                return;
            if (TextCheck())
            {
                ReadingInfo read = new ReadingInfo
                {
                    RID = this.textBoxReadingID.Text,
                    RName = this.textBoxName.Text,
                    Raddress = this.textBoxRaddress.Text,
                    Remark = this.textBRemark.Text
                };
                string sql = bll.insertReading(read);
                MessageBox.Show(sql, "系统提示");
                selectReading();
                textClear();
            }
        }
        //修改书架信息
        public void UpdateReading()
        {
            if (TextCheck())
            {
                ReadingInfo read = new ReadingInfo
                {
                    RID = this.textBoxReadingID.Text,
                    RName = this.textBoxName.Text,
                    Raddress = this.textBoxRaddress.Text,
                    Remark = this.textBRemark.Text
                };
                string sql = bll.updataReading(read);
                MessageBox.Show(sql, "系统提示");
                selectReading();
                textClear();
            }
        }
        #endregion

        private void FrmReading_Load(object sender, EventArgs e)
        {
            selectReading();
        }

        private void skinbtnAddreading_Click(object sender, EventArgs e)
        {
            InsertReading();
        }

        private void skinbtnUpdate_Click(object sender, EventArgs e)
        {
            UpdateReading();
        }

        private void skinbtnselect_Click(object sender, EventArgs e)
        {
            GetName();
        }

        private void listViewReadingInfo_Click(object sender, EventArgs e)
        {
            LVClick();
        }
        //检测账号输入
        public bool TextCheck(KeyPressEventArgs e)
        {
            //数字，大写英文，小写英文，删除键
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || ((Keys)(e.KeyChar) == Keys.Back))
            {
                return false;
            }
            return true;

        }
        private void textBoxReadingID_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = TextCheck(e);
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            textClear();
        }

        
    }
}
